﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

public partial class CardCenter_PurchaseHistoryQuery : System.Web.UI.Page
{
    //获取购卡历史
    BLL.PurchasedCardHistoryQuery history = new PurchasedCardHistoryQuery();
    WebUtility util = new WebUtility();
    ConsultationCardPwdSelect bllCardInfo = new ConsultationCardPwdSelect();
    BLL.ReadParamtes read = new ReadParamtes();
    BLL.SendSMS sendSms = new SendSMS();
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            //选择开始及结束时间
            this.txt_StartTime.Attributes.Add("onfocus", "WdatePicker({skin:'ext',dateFmt:'yyyy-MM-dd HH:mm:ss'})");
            this.txt_EndTime.Attributes.Add("onfocus", "WdatePicker({skin:'ext',dateFmt:'yyyy-MM-dd HH:mm:ss'})");
        }

    }

    //查询购卡历史
    protected void ImgBtnQuery_Click(object sender, ImageClickEventArgs e)
    {
        int smsLength = 0;
        string mobile = "";
        string startTime = "";
        string endTime = "";
        if(this.MobileNO.Text != null && this.MobileNO.Text != "")
        {
            mobile = this.MobileNO.Text;
        }

        if(!string.IsNullOrEmpty(this.txt_StartTime.Text) && !string.IsNullOrEmpty(this.txt_EndTime.Text))
        {
            startTime = this.txt_StartTime.Text.Trim(); ;
            endTime = this.txt_EndTime.Text.Trim(); ;
        }
        //获取验证码
        string verifyCode = this.txtValidateCode.Text.Trim();
        //验证验证码是否过期
        bool isRequestTimeOut = bllCardInfo.IsRequestTimedOut(mobile, verifyCode);
        if (isRequestTimeOut == false)
        {
            this.txtValidateCode.Text = "";
            this.txt_EndTime.Text = "";
            this.txt_StartTime.Text = "";
            this.MobileNO.Text = "";
            util.AlertMsg("对不起，您所输入的验证码已过期！请重新申请。");
        }
        else 
        {
            bool isSame = bllCardInfo.IsSameToVerifyCodeAndMobileNO(mobile, verifyCode);
            if (isSame == false)
            {
                this.txtValidateCode.Text = "";
                this.txt_EndTime.Text = "";
                this.txt_StartTime.Text = "";
                this.MobileNO.Text = "";
                util.AlertMsg("对不起，您所输入的验证码错误！请重新申请。");
            }
            //验证码未过期并且正确
            else 
            {
                // order by S_Time ASC
                //================网页方式呈现购卡历史=====================================
                string whereConditions = "(S_MobileNO = '" + mobile + "') and (S_Time between '" + startTime + "' and '" + endTime + "')";
                Session["queryPurchase"] = whereConditions;
                Response.Redirect("QueryResult.aspx");

                //================发送短信方式呈现购卡历史=================================
                //BLL.Cards[] card = history.GetUserPurchaseHistory(mobile, startTime, endTime);
                ////读取短信头模板并生成头消息
                //string header = read.getParamValue("SmsPurchaseHistoryHeader");
                //if (card != null && card.Length != 0)
                //{
                //    int k = card.Length;
                //    header = WebUtility.GenratePurchaseCardHistorySmsHeader(k, header);
                //    //发送消息头
                //    sendSms.SendMsg(header, card[0].Mobile,"1");
                //    //读取购卡历史短信模板并生成消息
                //    string smsBody = read.getParamValue("SmsPurchaseHistoryBody");
                //    string sendValue = "";
                //    smsLength = card.Length;
                //    for (int i = 0; i < card.Length; i++)
                //    {
                //        sendValue = WebUtility.GenratePurchaseCardsHistorySms(card[i].PurchaseTime, card[i].CardSeriaNumber, card[i].CardMoney, card[i].CardPoint, card[i].CardPayType, smsBody);
                //        //发送短信
                //        sendSms.SendMsg(sendValue, card[i].Mobile,"1");
                //    }

                //    Image1.ImageUrl = "~/Images/CardCenterImg/hui1.jpg";
                //    Image3.ImageUrl = "~/Images/CardCenterImg/cai2.jpg";
                //    bu1.ForeColor = System.Drawing.Color.Gray;
                //    bu2.ForeColor = System.Drawing.Color.Black;
                //    //给用户提示
                //    util.AlertMsg("将有" + smsLength + "条购卡记录发送到您的手机请注意查收！");
                //}
                //else
                //{
                //    util.AlertMsg("没有查到手机号为" + mobile + "用户的购卡记录！");
                //}
                //-------------------
            }
        }
    }

    //----------------------------------------------------
}
